package com.stray.crawl.expr;

import com.stray.crawl.ResultContext;
import com.stray.crawl.TaskContext;

public class EventData {

	protected static final String DELIMITER = ",";

	private TaskContext taskContext;
	private ResultContext resultContext;
	private Object object;
	private String arguments;

	public TaskContext getTaskContext() {
		return taskContext;
	}

	public void setTaskContext(TaskContext taskContext) {
		this.taskContext = taskContext;
	}

	public ResultContext getResultContext() {
		return resultContext;
	}

	public void setResultContext(ResultContext resultContext) {
		this.resultContext = resultContext;
	}

	public Object getObject() {
		return object;
	}

	public void setObject(Object object) {
		this.object = object;
	}

	public String getArguments() {
		return arguments;
	}

	public void setArguments(String arguments) {
		this.arguments = arguments;
	}

	public int toIntArgument() {
		if (arguments != null && arguments.length() > 0) {
			String pArgs = arguments.trim();

			for (int i = 0; i < pArgs.length(); i++) {
				char c = arguments.charAt(i);
				if (!Character.isDigit(c)) {
					return -1;
				}
			}

			return Integer.valueOf(arguments);
		}

		return -1;
	}

	public int[] toIntArrayArgument() {
		int[] intArray = null;

		if (arguments != null && arguments.length() > 0) {
			String[] strArray = arguments.split(DELIMITER);
			intArray = new int[strArray.length];

			for (int i = 0; i < strArray.length; i++) {
				String str = strArray[i].trim();
				boolean valid = true;

				for (int j = 0; j < str.length(); j++) {
					char c = str.charAt(j);

					if (!Character.isDigit(c)) {
						valid = true;
						break;
					}
				}

				intArray[i] = valid ? Integer.valueOf(str) : 0;
			}
		}

		return intArray;
	}

	public String[] toStringArrayArgument() {
		String[] retArray = null;

		if (arguments != null && arguments.length() > 0) {
			String[] strArray = arguments.split(DELIMITER);
			retArray = new String[strArray.length];

			for (int i = 0; i < strArray.length; i++) {
				retArray[i] = strArray[i].trim();
			}
		}

		return retArray;
	}

}
